package com.baway.biliclitent.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.baway.biliclitent.R;
import com.baway.biliclitent.adapter.LiveFragRecyAdapter;
import com.baway.biliclitent.model.LiveBean;
import com.baway.biliclitent.util.CommonUtil;
import com.baway.biliclitent.util.OkHttpUtils;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播 Fragment
 */

public class FragmentLive extends Fragment {

    private View view;
    private Banner home_head_banner;
    private OkHttpUtils httpUtils;
    private String url = "http://live.bilibili.com/AppIndex/home?_device=android&_hwid=51e96f5f2f54d5f9&_ulv=10000&access_key=563d6046f06289cbdcb472601ce5a761&appkey=c1b107428d337928&build=410000&platform=android&scale=xxhdpi&sign=fbdcfe141853f7e2c84c4d401f6a8758";
    private RecyclerView live_frag_recyclerView;
    private SwipeRefreshLayout swipe_container;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_live, container, false);
        initViews();
        return view;
    }

    //初始化控件
    private void initViews() {
        httpUtils = OkHttpUtils.getHttpUtils();
        live_frag_recyclerView = (RecyclerView) view.findViewById(R.id.live_frag_recyclerView);
        swipe_container = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        swipe_container.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipe_container.setRefreshing(false);
                    }
                },200);
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (CommonUtil.isNetworkAvailable(getContext())) {
            getData(url);
        }else{
            Toast.makeText(getContext(),"当前没有网络连接",Toast.LENGTH_SHORT).show();
        }
    }

    private void getData(String path) {
        httpUtils.loadDataFromNet(path, LiveBean.class, new OkHttpUtils.CallBackListener<LiveBean>() {
            @Override
            public void onSuccess(LiveBean result) {
                List<String> images = new ArrayList<>();
                //得到轮播的图片路径集合
                List<LiveBean.DataBean.BannerBean> banner = result.getData().getBanner();
                List<LiveBean.DataBean.PartitionsBean> partitions = result.getData().getPartitions();
                if (banner != null&&partitions != null) {
                    for (int i = 0; i < banner.size(); i++) {
                        images.add(banner.get(i).getImg());
                    }
                    centerRecy(partitions,images);
                } else {
                    return;
                }
            }

            @Override
            public void onFail() {

            }
        });
    }

    //直播分类以及直播个数
    public void centerRecy(List<LiveBean.DataBean.PartitionsBean> partitions,List<String> imageList) {
        live_frag_recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        LiveFragRecyAdapter adapter = new LiveFragRecyAdapter(partitions, getContext(),imageList);
        live_frag_recyclerView.setAdapter(adapter);
    }
}